<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Ruby Overloading Methods: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="ruby study notes,ruby overloading methods,ruby guide,free ruby programming course,best ruby guide,ruby tutorials,ruby tutorial,learn ruby,ruby,ruby on rails,ruby rails,ruby learning,ruby tutoring,learning ruby,ruby programming,ruby on rails development,ruby training" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />
<link rel="stylesheet" href="http://rubylearning.github.io/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="http://rubylearning.github.io/images/syntaxhighlighter.css" type="text/css" />
<link rel="icon" type="image/ico" href="http://rubylearning.github.io/images/favicon.ico" />
<!-- Google +1 button code -->
<link rel="canonical" href="http://rubylearning.github.io/satishtalim/ruby_overloading_methods.html" />
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

<!-- Google Analytics code -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Google Analytics code ends -->
</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Helping Ruby Programmers become Awesome!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="http://rubylearning.github.io/" title="Home page for rubylearning.github.io">Home</a></li>
            <li><a href="http://rubylearning.github.io/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="http://rubylearning.github.io/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="http://rubylearning.github.io/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="http://rubylearning.github.io/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="http://rubylearning.github.io/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="http://rubylearning.github.io/jobs/ruby_jobs.html" title="Find Ruby Jobs and also post your job requirements">Jobs</a></li>
            <li><a href="http://rubylearning.github.io/blog/" title="Ruby blog of Learning Ruby site">Blog</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Online Ruby Course">Online Course</a></li>
            <li><a href="http://ruby-challenge.rubylearning.org/" title="Ruby Programming Challenge for Newbies">Challenge</a></li>
            <li><a href="http://rubylearning.github.io/satishtalim/services.html" title="Services offered by Satish Talim">Services</a></li>
            <li><a href="http://rubylearning.github.io/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">

            <div id="main">

                <div id="main-inner"><a name="TemplateInfo"></a>
                <h1>Ruby Overloading Methods</h1>
                <!-- Place this tag where you want the +1 button to render -->
				<div class="g-plusone"></div>
				<script type="text/javascript">
				  window.___gcfg = {
				    lang: 'en-US'
				  };

				  (function() {
				    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
				    po.src = 'https://apis.google.com/js/plusone.js';
				    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
				  })();
				</script>

                <p><!-- Google advt starts-->
                   <!-- Google ad New Ruby Site -->
                   <br />
                   <script type="text/javascript"><!--
                   google_ad_client = "pub-2265705826025555";
                   google_ad_width = 468;
                   google_ad_height = 60;
                   google_ad_format = "468x60_as";
                   google_ad_type = "text";
                   //2007-05-10: RLROM
                   google_ad_channel = "4256575930";
                   google_color_border = ["FFFFFF","FFFFFF"];
                   google_color_bg = "FFFFFF";
                   google_color_link = ["B37400","B37400"];
                   google_color_url = ["B37400","B37400"];
                   google_color_text = ["000000","000000"];
                   //--></script>
                   <script type="text/javascript"
                     src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
                   </script>
                   <!-- Google advt ends -->
                </p>
                <p class="post-footer align-right">
                  <strong>
                    <a href="http://rubylearning.github.io/satishtalim/ruby_overriding_methods.html">&lt;Overriding Methods | </a>
                    <a href="http://rubylearning.github.io/satishtalim/tutorial.html">TOC | </a>
                    <a href="http://rubylearning.github.io/satishtalim/ruby_access_control.html">Access Control&gt;</a>
                  </strong>
                </p>

                <p><strong>You want to create two different versions of a method with the same name: <em>two methods that differ in the arguments they take</em></strong>. However, <i>a Ruby class can have only one method with a given name</i> (if you define a method with the same name twice, the latter method definition prevails as seen in example <strong>p038or.rb</strong> in topic <a href="http://rubylearning.github.io/satishtalim/ruby_overriding_methods.html">Ruby Overriding Methods</a>). Within that single method, though, you can put logic that branches depending on how many and what kinds of objects were passed in as arguments.</p>

                <p>Here's a Rectangle class that represents a rectangular shape on a grid. You can instantiate a Rectangle by one of two ways: by passing in the coordinates of its top-left and bottom-right corners, or by passing in its top-left corner along with its length and width. There's only one <strong>initialize</strong> method, but you can act as though there were two.</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # The Rectangle constructor accepts arguments in either
                # of the following forms:
                #   Rectangle.new([x_top, y_left], length, width)
                #   Rectangle.new([x_top, y_left], [x_bottom, y_right])
                class Rectangle
                  def initialize(*args)
                    if args.size &lt; 2  || args.size &gt; 3
                      # modify this to raise exception, later
                      puts 'This method takes either 2 or 3 arguments'
                    else
                      if args.size == 2
                        puts 'Two arguments'
                      else
                        puts 'Three arguments'
                      end
                    end
                  end
                end
                Rectangle.new([10, 23], 4, 10)
                Rectangle.new([10, 23], [14, 13])
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>The above program <strong>p037rectangle.rb</strong> is incomplete from the Rectangle class viewpoint, but is enough to demonstrate how method overloading can be achieved. Also remember that the <strong>initialize</strong> method takes in a variable number of arguments.</p>

                <h3>Summary</h3>

                <p>I have listed down all the <strong><a href="http://rubylearning.github.io/satishtalim/ruby_summary5.html">important points</a></strong> you need to remember after you have completed the following topics: <a href="http://rubylearning.github.io/satishtalim/ruby_regular_expressions.html">Regular Expressions</a>, <a href="http://rubylearning.github.io/satishtalim/writing_our_own_class_in_ruby.html">Writing our own Class</a>, <a href="http://rubylearning.github.io/satishtalim/ruby_method_missing.html">Method Missing</a>, <a href="http://rubylearning.github.io/satishtalim/ruby_procs.html">Ruby Procs</a>, <a href="http://rubylearning.github.io/satishtalim/including_other_files_in_ruby.html">Including other files</a>, <a href="http://rubylearning.github.io/satishtalim/ruby_open_classes.html">Ruby Open Classes</a>, <a href="http://rubylearning.github.io/satishtalim/ruby_inheritance.html">Inheritance</a>, <a href="http://rubylearning.github.io/satishtalim/ruby_overriding_methods.html">Overriding Methods</a>, <a href="http://rubylearning.github.io/satishtalim/ruby_overloading_methods.html">Overloading Methods</a>.</p>

                <p style="background-color: #FAFAFA; padding: 5px; margin-top: 20px; font-size: 65%;"><strong>Note</strong>: The Ruby Logo is Copyright (c) 2006-2020 <span style="font-weight: bold;" title="Click Gurus on the menu above">Ruby Gurus</span>), my acknowledgment and thanks to all of them. Much of the material on <a href="http://rubylearning.github.io/">rubylearning.github.io</a> and in the course at <a href="http://rubylearning.org/">rubylearning.org</a> is drawn <strong>primarily</strong> from the <strong>Programming Ruby book</strong>, available from <a href="http://pragprog.com/book/ruby3/programming-ruby-1-9">The Pragmatic Bookshelf</a>.</p>

                <p class="post-footer align-right">
                  <strong>
                    <a href="http://rubylearning.github.io/satishtalim/ruby_overriding_methods.html">&lt;Overriding Methods | </a>
                    <a href="http://rubylearning.github.io/satishtalim/tutorial.html">TOC | </a>
                    <a href="http://rubylearning.github.io/satishtalim/ruby_access_control.html">Access Control&gt;</a>
                  </strong>
                </p>

            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">

                <p><!-- Google advt starts-->
                   <!-- Google ad New Ruby Site -->
                   <script type="text/javascript"><!--
                   google_ad_client = "pub-2265705826025555";
                   google_ad_width = 120;
                   google_ad_height = 600;
                   google_ad_format = "120x600_as";
                   google_ad_type = "text";
                   //2007-05-10: RLROM
                   google_ad_channel = "4256575930";
                   google_color_border = ["FFFFFF","FFFFFF"];
                   google_color_bg = "FFFFFF";
                   google_color_link = ["B37400","B37400"];
                   google_color_url = ["B37400","B37400"];
                   google_color_text = ["000000","000000"];
                   //--></script>
                   <script type="text/javascript"
                     src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
                   </script>
                   <!-- Google advt ends -->
                </p>

            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p> &copy; 2006-2021 <strong>rubylearning.github.io A Ruby Tutorial</strong>&nbsp;&nbsp;

        Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://rubylearning.github.io/">Home</a> | <a href="http://rubylearning.github.io/privacy.html">Privacy</a> | <a href="http://rubylearning.github.io/sitemap.html">Sitemap</a> </p>
</div>

<!-- footer ends here -->
<!-- SyntaxHighlighter code -->
<script src="http://rubylearning.github.io/js/shCore.js" type="text/javascript"></script>
<script src="http://rubylearning.github.io/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->
</body>
</html>
